import time
import os
from bs4 import BeautifulSoup


# 连接网络
# name是网络名称。username是电话
def connect(name,username):
    password = "123123"
    cmd_str = "rasdial %s %s %s" % (name, username, password)
    res = os.system(cmd_str)
    if res == 0:
        print("connect successful")
    else:
        print('fail')
    time.sleep(5)
    return name


# 断网
def disconnect(name):
    cmdstr = "rasdial %s /disconnect" % name
    os.system(cmdstr)
    time.sleep(5)


# 如果断网，重连
# 通过网页内容判断（火狐）
def juge_internet(html):
    soup = BeautifulSoup(html, 'lxml')
    try:
        inter=soup.findAll('div',{'class':'title'})[0].get_text(strip=True)
        # 断网了
        if inter=='呃…找不到这个网站。':
            disconnect('jsw')
            connect('jsw','18297972183')
            return False
        else:
            return True
    except:
        return True